<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-left">
      <div class="jsbos-common-title">
        <h2>{{ $t("common.organization") }}</h2>
        <span class="options">
          <el-dropdown>
            <el-link icon="icon-ym icon-ym-mpMenu" :underline="false" />
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="getOrganizeList()"
                >刷新数据</el-dropdown-item
              >
              <el-dropdown-item @click.native="toggleExpand(true)"
                >展开全部</el-dropdown-item
              >
              <el-dropdown-item @click.native="toggleExpand(false)"
                >折叠全部</el-dropdown-item
              >
              <el-dropdown-item @click.native="showDiagram"
                >架构图</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </span>
      </div>
      <div class="jsbos-common-tree-search-box">
        <el-input
          placeholder="输入关键字"
          v-model="filterText"
          suffix-icon="el-icon-search"
          clearable
        />
      </div>
      <el-scrollbar
        class="jsbos-common-el-tree-scrollbar"
        v-loading="treeLoading"
      >
        <el-tree
          ref="treeBox"
          :data="treeData"
          :props="defaultProps"
          :default-expanded-keys="expandKeyList"
          highlight-current
          :expand-on-click-node="false"
          node-key="id"
          @node-click="handleNodeClick"
          class="jsbos-common-el-tree"
          v-if="refreshTree"
          :filter-node-method="filterNode"
        >
          <span
            class="custom-tree-node"
            slot-scope="{ data, node }"
            :title="data.fullName"
          >
            <i :class="data.icon" />
            <span class="text" :title="data.fullName">{{ node.label }}</span>
          </span>
        </el-tree>
      </el-scrollbar>
    </div>
    <div class="jsbos-common-layout-center jsbos-flex-main">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input
                v-model="listQuery.keyword"
                placeholder="请输入姓名"
                clearable
                @keyup.enter.native="search()"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="form-BtnNowarp">
              <el-button type="primary" icon="el-icon-search" @click="search()">
                {{ $t("common.search") }}</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >{{ $t("common.reset") }}
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              v-has="'btn_add'"
              >新建</el-button
            >
            <!-- <topOpts @add="addOrUpdateHandle()"> -->
            <el-button
              type="text"
              icon="el-icon-download"
              @click="exportForm"
              v-has="'btn_download'"
              >导出</el-button
            >
            <el-button
              type="text"
              icon="el-icon-upload2"
              @click="uploadForm"
              v-has="'btn_upload'"
              >导入</el-button
            >
          </div>
          <!-- </topOpts> -->
          <div class="jsbos-common-head-right">
            <el-tooltip
              effect="dark"
              :content="$t('common.refresh')"
              placement="top"
            >
              <el-link
                icon="icon-ym icon-ym-Refresh jsbos-common-head-icon"
                :underline="false"
                @click="initData()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table v-loading="listLoading" :data="tableData">
          <el-table-column prop="code" label="工号" width="100" />
          <el-table-column prop="realName" label="姓名" width="100">
            <template slot-scope="scope">
              <el-link
                @click="handleUserPreview(scope.row)"
                type="primary"
                :underline="false"
              >
                {{ scope.row.realName }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="account" label="账号" width="100" />
          <el-table-column
            prop="gender"
            label="性别"
            width="90"
            align="center"
            sortable
          >
            <template slot-scope="scope">
              <span>{{
                scope.row.gender == 1
                  ? "男"
                  : scope.row.gender == 2
                  ? "女"
                  : "保密"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="mobilePhone" label="手机" width="120" />
          <el-table-column
            prop="organize"
            label="所属组织"
            min-width="200"
            show-overflow-tooltip
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            :formatter="jsbos.tableDateFormat"
            width="120"
          />
          <el-table-column
            prop="enabledMark"
            label="状态"
            width="70"
            align="center"
          >
            <template slot-scope="scope">
              <el-tag
                type="success"
                disable-transitions
                v-if="scope.row.enabledMark == 1"
                >正常
              </el-tag>
              <el-tag
                type="warning"
                disable-transitions
                v-else-if="scope.row.enabledMark == 2"
                >锁定
              </el-tag>
              <el-tag type="danger" disable-transitions v-else>禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" v-has="'btn_operate'" >
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id)"
                v-has="'btn_edit'"
                >编辑</el-button
              >
              <el-button
                type="text"
                @click="handleDel(scope.row.id)"
                class="jsbos-table-delBtn"
                v-has="'btn_remove'"
                >删除
              </el-button>
              <!-- <tableOpts
                @edit="addOrUpdateHandle(scope.row.id)"
                @del="handleDel(scope.row.id)"
              > -->
              <el-dropdown hide-on-click>
                <span class="el-dropdown-link">
                  <el-button size="mini" type="text">
                    {{ $t("common.moreBtn")
                    }}<i class="el-icon-arrow-down el-icon--right"></i>
                  </el-button>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    @click.native="handleUserPreview(scope.row)"
                    v-has="'btn_detail'"
                  >
                    详情
                  </el-dropdown-item>
                  <el-dropdown-item
                    v-has="'btn_reset'"
                    @click.native="
                      handleResetPwd(scope.row.id, scope.row.account)
                    "
                  >
                    {{ $t("user.resetPassword") }}
                  </el-dropdown-item>
                  <el-dropdown-item
                    @click.native="handleUpdateState(scope.row)"
                    v-if="scope.row.enabledMark == 0"
                    v-has="'btn_disabled'"
                    >解除禁用
                  </el-dropdown-item>
                  <el-dropdown-item
                    @click.native="handleUpdateState(scope.row)"
                    v-if="scope.row.enabledMark == 1"
                    v-has="'btn_disabled'"
                    >禁用
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <!-- </tableOpts> -->
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
        <RolePreview v-if="rolePreviewVisible" ref="RolePreview" @close="removeRolePreview" />
      </div>
    </div>
    <Form v-show="formVisible" ref="Form" @close="removeForm" />
    <Diagram
      v-if="diagramVisible"
      ref="Diagram"
      @close="diagramVisible = false"
    />
    <ResetPwdForm
      v-if="resetFormVisible"
      ref="ResetPwdForm"
      @refreshDataList="initData"
    />
    <ExportForm v-if="exportFormVisible" ref="exportForm" v-dialogDrag />
    <ImportForm
      v-if="importFormVisible"
      ref="importForm"
      @refresh="reset()"
      v-dialogDrag
    />
  </div>
</template>
<script>
import { getDepartmentSelector,getOrganizeZhiXingList } from "@/api/permission/department";
import {
  getZhiXingUserList,
  getUserList,
  updateUserState,
  unlockUser,
  delUser,
} from "@/api/permission/user";
import request from "@/utils/request";
import Form from "./Form";
import Diagram from "./Diagram";
import ResetPwdForm from "./ResetPassword";
import ImportForm from "./ImportForm";
import ExportForm from "./ExportForm";
import { isEmpty } from "@/utils/isEmpty";
import RolePreview from './profile/index.vue'
export default {
  name: "permission-user",
  components: {
    Form,
    Diagram,
    ResetPwdForm,
    ExportForm,
    ImportForm,
    RolePreview
  },
  data() {
    return {
      stateAdmin: false,
      keyword: "",
      treeData: [],
      expandKeyList:[],
      departmentsData: [],
      tableData: [],
      treeLoading: false,
      listLoading: true,
      isDetail: false,
      listQuery: {
        organizeId: "",
        organizeIds: [],
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        menuId: ""
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      total: 0,
      type: "",
      formVisible: false,
      diagramVisible: false,
      resetFormVisible: false,
      rolePreviewVisible: false,
      authorizeFormVisible: false,
      importFormVisible: false,
      exportFormVisible: false,
      expands: false,
      refreshTree: true,
      filterText: "",
    };
  },
  watch: {
    filterText(val) {
      this.$refs.treeBox.filter(val);
    },
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.getOrganizeList(true);
  },
  methods: {
    showDiagram() {
      this.diagramVisible = true;
      this.$nextTick(() => {
        this.$refs.Diagram.init();
      });
    },
    search() {
      this.listQuery.currentPage = 1;
      this.listQuery.pageSize = 20;
      this.listQuery.sort = "desc";
      this.initData();
    },
    reset() {
      this.listQuery.keyword = "";
      this.search();
    },
    toggleExpand(expands) {
      this.refreshTree = false;
      this.expands = expands;
      this.$nextTick(() => {
        this.refreshTree = true;
        this.$nextTick(() => {
          this.$refs.treeBox.setCurrentKey(this.companyId);
        });
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.fullName.indexOf(value) !== -1;
    },
    getOrganizeList(isInit) {
      this.treeLoading = true;
      this.listQuery.menuId = this.menuId
      request({
        url: "/api/permission/Organize/UserDepartment/Selector/" + this.menuId,
        method: "get",
      })
        .then((res) => {
          this.treeData = res.data.list;

          this.expandKeyList=res.data.list.map(item=>{
            return item.id
          })
          if (
            !isEmpty(this.treeData)
          ) {

            this.listQuery.organizeId =this.treeData[0].id
            // this.listQuery.organizeIds = this.departmentsData;
          } else {
            this.listQuery.organizeId = null;
          }
          this.treeLoading = false;
          this.initData();
        })
        .catch(() => {
          this.treeLoading = false;
        });
    },
    initData() {
      this.listLoading = true;
      getZhiXingUserList(this.listQuery)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    handleNodeClick(data) {
      //点击公司，不查询
      if (this.listQuery.organizeId === data.id) return;
      this.listQuery.organizeId = data.id;
      // this.listQuery.organizeIds= this.departmentsData
      this.type = data.type;
      this.reset();

    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.init(
          id,
          isDetail,
          this.treeData,
          this.type === "department" ? this.listQuery.organizeId : ""
        );
      });
    },
    removeForm(isRefresh) {
      this.formVisible = false;
      if (isRefresh) {
        this.keyword = "";
        this.initData();
      }
    },
    removeAuthorizeForm(isRefresh) {
      this.authorizeFormVisible = false;
      if (isRefresh) {
        this.keyword = "";
        this.initData();
      }
    },
        removeRolePreview(isRefresh) {
          this.rolePreviewVisible = false;
          if (isRefresh) {
            this.keyword = "";
            this.initData();
          }
        },
    handleUpdateState(row) {
      const txt = row.enabledMark ? "禁用" : "开启";
      this.$confirm(`您确定要${txt}当前用户吗, 是否继续?`, "提示", {
        type: "warning",
      })
        .then(() => {
          updateUserState(row.id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              duration: 1000,
              onClose: () => {
                row.enabledMark = row.enabledMark ? 0 : 1;
              },
            });
          });
        })
        .catch(() => {});
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          delUser(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              duration: 1500,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    unlockUser(id) {
      this.$confirm("此操作将解除该账户锁定, 是否继续?", "解除锁定", {
        type: "warning",
      })
        .then(() => {
          unlockUser(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              duration: 1500,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    exportForm() {
      this.exportFormVisible = true;
      this.$nextTick(() => {
        this.$refs.exportForm.init(this.listQuery);
      });
    },
    uploadForm() {
      this.importFormVisible = true;
      this.$nextTick(() => {
        this.$refs.importForm.init();
      });
    },
    handleResetPwd(id, account) {
      this.resetFormVisible = true;
      this.$nextTick(() => {
        this.$refs.ResetPwdForm.init(id, account);
      });
    },
        //权限预览
        handleUserPreview(row) {
          this.rolePreviewVisible = true
          this.$nextTick(() => {
            this.$refs.RolePreview.init(row.id, row.realName, 'User')
          })
        }
  },
};
</script>
